package com.hoshiicloud.common.generator.id.service;

import com.baidu.fsg.uid.UidGenerator;
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class UidGenService {

    @Resource
    private UidGenerator uidGenerator;

    public long getUid() {
        return uidGenerator.getUID();

    }

    public Set<String> getUidList(int count) {
        if (count <= 0) {
            return null;
        }
        Set<String> uidSet = new TreeSet<>();
        for (int i = 0; i < count; i++) {
            uidSet.add(String.valueOf(uidGenerator.getUID()));
        }
        log.debug("要求产生" + count + "个id,实际产生了" + uidSet.size() + "个id");
        if (count != uidSet.size()) {
            log.error("严重错误:产生重复id!!!");
            return null;
        }
        log.info("id列表:" + uidSet.toString());
        return uidSet;
    }

    public Long getComUserCode() {
        // 定义变长字符串
        StringBuilder str = new StringBuilder();
        // 随机数
        Random random = new Random();
        // 随机生成（1-9）的数字，并添加到字符串
        for (int i = 0; i < 10; i++) {
            int temp = random.nextInt(10);
            if (temp == 0) {
                temp = 1;
            }
            str.append(temp);
        }
        return Long.parseLong(str.toString());
    }

    public Long getInviteCode() {
        // 定义变长字符串
        StringBuilder str = new StringBuilder();
        // 随机数
        Random random = new Random();
        // 随机生成（1-9）的数字，并添加到字符串
        for (int i = 0; i < 7; i++) {
            int temp = random.nextInt(10);
            if (temp == 0) {
                temp = 1;
            }
            str.append(temp);
        }
        return Long.parseLong(str.toString());
    }
}
